package com.latvia.ui;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.astuetz.viewpager.extensions.PagerSlidingTabStrip;
import com.latvia.cookingapp.R;
import com.latvia.managers.AppManager;
import com.latvia.model.Recipe;

public class AllCategoriesFragment extends Fragment {

	private PagerSlidingTabStrip tabs;
	private ViewPager pager;
	private MyPagerAdapter adapter;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		//example layout - it's not seriously)
		View layout = inflater.inflate(R.layout.all_categories_fragment,
				container, false);
//		Button viewRecipe = (Button)layout.findViewById(R.id.viewRecipe);
//		
//		OnClickListener oclViewRecipe = new OnClickListener() {
//			@Override
//			public void onClick(View v) {
//				 Intent myIntent = new Intent(getActivity(), AllRecipesActivity.class);
//				   getActivity().startActivity(myIntent);
//			}
//		};
//		
//		viewRecipe.setOnClickListener(oclViewRecipe);
		
		tabs = (PagerSlidingTabStrip) layout.findViewById(R.id.tabs);
		tabs.setIndicatorColorResource(R.color.GreenAlpha);
		tabs.setDividerColorResource(R.color.GreenAlpha);
		pager = (ViewPager) layout.findViewById(R.id.pager);
		adapter = new MyPagerAdapter(getChildFragmentManager());

		pager.setAdapter(adapter);


		final int pageMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4, getResources()
				.getDisplayMetrics());
		pager.setPageMargin(pageMargin);

		tabs.setViewPager(pager);
		tabs.setShouldExpand(true);
		
		
		AppManager.getInstance().setCurrentTab(3);
		
		return layout;
	}
	
//	public void reload(ArrayList<Recipe> recipes) {
//		for (int i = 0; i < 3; i++) {
//			RecipeCategoryFragment rcf = (RecipeCategoryFragment)adapter.getItem(i);
//			rcf.reload(recipes);
//		}
//		
//		adapter.notifyDataSetChanged();
//	}
//	

	
	public class MyPagerAdapter extends FragmentPagerAdapter {

		private final List<String> titles = AppManager.getInstance().getRecipeCategories();
		public MyPagerAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public CharSequence getPageTitle(int position) {
			return titles.get(position);
		}

		@Override
		public int getCount() {
			return titles.size();
		}

		@Override
		public Fragment getItem(int position) {
			return RecipeCategoryFragment.newInstance(position);
		}

	}

}
